import GAMEFUNC_ from "./func.js";
import RoleClass from "./Role.js";
import MAIN_CANVAS from "./CANVAS.js";
import runtime_library from "./runtime.js";

export default class Block {

    //地图块

    static MAIN_RENDERER(){

    }

    static TICK(runtime){

        if(!GAMEFUNC_.RUNTIME.IN_GAME_LAYOUT(runtime)) {return}
            this.MAIN_EVENTHANDLER_(runtime)

    }

    static RUNTIME_LAYOUT_START(runtime)
    {
        if(!GAMEFUNC_.RUNTIME.IN_GAME_LAYOUT(runtime)) {return}
            this.INIT_EVENTHANDLER(runtime)


    }

    static MAIN_EVENTHANDLER_(runtime)
    {
         this.RENDERINGMODE_.eventhandler(runtime)
        this.CANVAS.EVENTHANDLER(runtime)
        this.RUNTIME_INPUT.EVENTHANDLER(runtime)


    }

    static INIT_EVENTHANDLER(runtime)
    {

        this.CANVAS.INIT_EVENTHANDLER(runtime)
        this.DATA.INIT_DATA(runtime)



    }


    //数据

    static DATA={

        INIT_DATA(runtime) {

            Block.MAX_SEE_DISTANCE=200
            Block.MAX_REACH_DISTANCE=50

        }
    }


    static  CANVAS ={

        EVENTHANDLER(runtime)
        {

        },





        async INIT_EVENTHANDLER(runtime)
        {
            //await  GAMEFUNC_.RUNTIME_TIME_EVENTS_.FLOW_ACTIVE_TRANSPORT(500)

            //Block.CANVAS.rendering_on_screen(runtime)

        },


        async rendering_on_screen(runtime) {

            MAIN_CANVAS.TEXT_RUNTIME.INIT_TEXT(runtime)


            for(let block of runtime.objects.Block.instances())
            {
                let renderingConfig =[block.x-15,block.y+30,"main"]
                let TEXT =MAIN_CANVAS.TEXT_RUNTIME.NEW(runtime,renderingConfig)


                TEXT.text="name: "+block.uid
                TEXT.opacity=0.8
                TEXT.sizePt=5
            }




        }
    }




    static RENDERINGMODE_={

        //渲染

        eventhandler(runtime)
        {

            let instance =runtime.objects.f_block.instances()
            for(let BLOCK_ of instance)
            {

                BLOCK_.instVars.DIS=GAMEFUNC_.MATH.GET_DISTANCE(runtime,BLOCK_.x,BLOCK_.y,RoleClass.RoleSprite.x,RoleClass.RoleSprite.y)
            }

            Block.RENDERINGMODE_.VisibilityRender(runtime)
            Block.RENDERINGMODE_.ReachabilityRender(runtime)
            Block.RENDERINGMODE_.outlinerender(runtime)



        },




        VisibilityRender(runtime){

            let instance =runtime.objects.f_block.instances()
            for(let BLOCK_ of instance)
            {

                if(BLOCK_.instVars.DIS>Block.MAX_SEE_DISTANCE)
                {
                    BLOCK_.opacity=0
                }else
                {
                    BLOCK_.opacity=1
                }

            }

        },

        ReachabilityRender(runtime){

            let instance =runtime.objects.f_block.instances()

            for (let BLOCK_ of  instance)
            {
                if(BLOCK_.instVars.DIS<=Block.MAX_REACH_DISTANCE){
                    Block.SPRITE_ACTIONS.SET_ANIMATION(runtime,BLOCK_,"REACH")
                }

                if(BLOCK_.instVars.DIS>Block.MAX_REACH_DISTANCE){
                    Block.SPRITE_ACTIONS.SET_ANIMATION(runtime,BLOCK_,"NR")
                }
            }

        },


        outlinerender(runtime){

            for(let block of runtime.objects.f_block.instances())
            {
                if(block==null){

                    return
                }


                if (block.instVars.MOUSE_OVER===1)
                {
                        Block.EFFECT_FUNC.OUT_LINE(runtime,block,true)
                }

                if (block.instVars.MOUSE_OVER===0)
                {
                    Block.EFFECT_FUNC.OUT_LINE(runtime,block,false)

                }


            }

        }





    }


    static SPRITE_ACTIONS={

        SET_ANIMATION(runtime,block,name)
        {
            if(block!=null)
            {
                block.setAnimation(name,"beginning")
            }
        }

    }



    static EFFECT_FUNC={

        OUT_LINE(runtime,block,active)
        {
            if (active){
                block.effects[0].setParameter(1,2)

            }
            if(!active)
            {

                block.effects[0].setParameter(1,0)
            }

        }

    }



    static RUNTIME_INPUT={


       EVENTHANDLER(runtime)
       {


          
       },



        _CLICK_BLOCK(runtime)
        {

            let block =GAMEFUNC_.INSTANCE_CLASS.GET_PICK_INSTANCE("f_block",runtime)
            RoleClass.ROLE_ACTION.ROLE_MOVE(runtime,block)



        },


        _MOUSE_OUVERTED(runtime)
        {

                let GET_BLOCK=GAMEFUNC_.INSTANCE_CLASS.GET_PICK_INSTANCE("f_block",runtime)




                if(GET_BLOCK.instVars.DIS>Block.MAX_REACH_DISTANCE){

                   return

                }

                runtime_library.MOUSE.SET_MOUSE_IMAGE(runtime,"hand")
                GET_BLOCK.instVars.MOUSE_OVER=1







        },

        _MOUSE_OFF_OVERTED(runtime){

           for(let GET_BLOCK of runtime.objects.f_block.instances())
           {
               GET_BLOCK.instVars.MOUSE_OVER=0

           }

            runtime_library.MOUSE.SET_MOUSE_IMAGE(runtime,"normal")



        }


    }




}





